package com.ruoyi.crm.crawling.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.crm.crawling.domain.CrmCrawling;
import com.ruoyi.crm.crawling.service.ICrmCrawlingService;
import com.ruoyi.crm.crawling.util.DemoAutoNewsCrawler;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据抓取Controller
 * 
 * @author ruoyi
 * @date 2021-08-31
 */
@RestController
@RequestMapping("/crm/crawling")
public class CrmCrawlingController extends BaseController
{
    @Autowired
    private ICrmCrawlingService crmCrawlingService;

    /**
     * 查询数据抓取列表
     */
    @PreAuthorize("@ss.hasPermi('crm:crawling:list')")
    @GetMapping("/list")
    public TableDataInfo list(CrmCrawling crmCrawling)
    {
        startPage();
        List<CrmCrawling> list = crmCrawlingService.selectCrmCrawlingList(crmCrawling);
        return getDataTable(list);
    }

    /**
     * 导出数据抓取列表
     */
    @PreAuthorize("@ss.hasPermi('crm:crawling:export')")
    @Log(title = "数据抓取", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CrmCrawling crmCrawling)
    {
        List<CrmCrawling> list = crmCrawlingService.selectCrmCrawlingList(crmCrawling);
        ExcelUtil<CrmCrawling> util = new ExcelUtil<CrmCrawling>(CrmCrawling.class);
        return util.exportExcel(list, "数据抓取数据");
    }

    /**
     * 获取数据抓取详细信息
     */
    @PreAuthorize("@ss.hasPermi('crm:crawling:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(crmCrawlingService.selectCrmCrawlingById(id));
    }

    /**
     * 新增数据抓取
     * @throws Exception 
     */
    @PreAuthorize("@ss.hasPermi('crm:crawling:add')")
    @Log(title = "数据抓取", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmCrawling crmCrawling) throws Exception
    {
//    	DemoAutoNewsCrawler crawler = new DemoAutoNewsCrawler("crawl", true, crmCrawlingService);
//    	crawler.start(50);
    	
        return null;
    }

    /**
     * 修改数据抓取
     */
    @PreAuthorize("@ss.hasPermi('crm:crawling:edit')")
    @Log(title = "数据抓取", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmCrawling crmCrawling)
    {
        return toAjax(crmCrawlingService.updateCrmCrawling(crmCrawling));
    }

    /**
     * 删除数据抓取
     */
    @PreAuthorize("@ss.hasPermi('crm:crawling:remove')")
    @Log(title = "数据抓取", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(crmCrawlingService.deleteCrmCrawlingByIds(ids));
    }
}
